package com.fable.rfid;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.BRMicro.Tools;
import com.fable.cordova.Result;
import com.rfid.uhf.UhfReader;

import java.util.List;

/**
 * 主界面
 */
public class MainActivity extends Activity implements View.OnClickListener {

    private UhfReader reader;
    private String serialportPath = "/dev/ttyMT1";
    private int port = 12;

    private Button btnRead;
    private EditText txtEPC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initComponents();
    }

    /**
     * 初始化组件
     */
    private void initComponents() {
        btnRead = (Button) findViewById(R.id.btnRead);
        txtEPC = (EditText) findViewById(R.id.txtEPC);
        btnRead.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnRead:
                txtEPC.setText("");
                //readUserData();
                Result result = readEPC();
                if(!result.getSuccess()){
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                }else {
                    txtEPC.setText(result.getData().toString());
                }
                break;
            default:
                break;
        }
    }

    /**
     * 读取EPC
     */
    private Result readEPC() {
        Result result = new Result();
        try {
            reader = UhfReader.getInstance(serialportPath, port);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setCode("202");
            result.setMessage("获取UhfReader异常");

            return result;
        }

        List<String> epcList = reader.inventoryTag();
        String epc = "";
        if (epcList != null && !epcList.isEmpty()) {
            epc = epcList.get(0);
        }

        result.setSuccess(true);
        result.setCode("200");
        result.setMessage("读取完成");
        result.setData(epc);
        return result;
    }

    /**
     * 读取用户数据
     */
    private void readUserData() {

        try {
            reader = UhfReader.getInstance(serialportPath, port);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> epcList = reader.inventoryTag();
        if (epcList == null || epcList.isEmpty()) {
            Toast.makeText(this, "未读取到EPC", Toast.LENGTH_SHORT).show();
        } else {
            String epc = epcList.get(0);

            int membank = UhfReader.MEMBANK_USER; //存储区
            int address = 0;
            int length = 1;
            String password = "00000000";
            byte[] accessPassword = Tools.HexString2Bytes(password);

            byte[] data = reader.readData(membank, address, length, accessPassword);
            if (data != null && data.length > 1) {
                txtEPC.setText(Tools.Bytes2HexString(data, data.length) + "\n");
            } else {
                if (data != null) {
                    Toast.makeText(this, "读数据失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this, "读数据失败，返回null", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
